import { getStationNames, getShuntingAlertOptions, getAlarmPageInfos } from '@/api/alarmInfo'
import dayjs from 'dayjs'
import Echart from './echart.vue'
import { getAlarmConfig, modifyAlarmConfig, getStationNo } from './api'
// import './index.scss'
export default {
  data() {
    return {
      rules: {
        isSendToFront: [{ required: true, message: '请选择', trigger: 'change' }],
        isSendToHandPoint: [{ required: true, message: '请选择', trigger: 'change' }],
        isSendToTrain: [{ required: true, message: '请选择', trigger: 'change' }]
      },
      activeName: 'first',
      xAxis: [],
      alarmManageStation: 0,
      alarmInfo: {
        obj: {},
        stationName: '', // 站场名称
        alertStation: [], // 保存选中的报警类型值
        trainNo: '', // 机车号
        startTime: new Date(new Date().getTime() - 3600 * 1000 * 24),
        endTime: new Date(new Date().getTime()),
        page: 1,
        size: 10
      },
      modifyAlarmManageShow: false,
      modifyAlarmInfo: {
        isSendToFront: false,
        isSendToHandPoint: false,
        isSendToTrain: false,
        description: ''
      },
      stationOption: [],
      alarmManageData: [],
      listLoading: true,
      tableKey: 0,
      currentPage: 1,
      pageSize: 10,
      totalDataLength: 0,
      stationNameOptions: [],
      shuntingAlertOptions: [],
      rcvTableData: [],
      checkedOptions: [], // 存储勾选的值
      tableHeight: 0
    }
  },
  components: {
    Echart
  },
  created() {
    this.getAllStationNames()
    this.getAllShuntingAlertOptions()
    this.getAllTrainInfos()
    // this.getAlarmManageData()
    this.getStationNoName()
  },
  mounted() {
    this.$nextTick(() => {
      // 根据浏览器高度设置初始高度
      this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      // 监听浏览器高度变化，修改表格高度
      window.onresize = () => {
        this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      }
    })
  },
  computed: {},
  watch: {
    startTime: function(newTime, oldTime) {
      if (!newTime) {
        this.startTime = new Date(new Date().getTime() - 3600 * 1000 * 24)
      }
    },
    endTime: function(newTime, oldTime) {
      if (!newTime) {
        this.endTime = new Date(new Date().getTime())
      }
    }
  },
  methods: {
    searchAlarmMange() {
      this.getAlarmManageData(this.alarmManageStation)
    },
    modifyAlarm(data) {
      this.modifyAlarmManageShow = true
      this.modifyAlarmInfo = data
    },
    postModifyAlarm() {
      this.$refs['modifyAlarmInfo'].validate(valid => {
        if (valid) {
          let newArr = []
          newArr.push(this.modifyAlarmInfo)
          modifyAlarmConfig(newArr).then(res => {
            if (res.code == 0) {
              // console.log(res.data, 'feg')
              this.modifyAlarmManageShow = false
              this.getAlarmManageData(this.alarmManageStation)
            } else {
              this.$message('修改失败')
            }
          })
        }
      })
    },
    getAlarmManageData(no) {
      let obj = {
        station: no
      }
      getAlarmConfig(obj).then(res => {
        if (res.code == 0) {
          // console.log(res.data[no], 'feg')
          this.alarmManageData = res.data[no]
        }
      })
    },
    getStationNoName() {
      getStationNo().then(res => {
        if (res.code == 0) {
          // console.log(res.data, 'feg')
          this.stationOption = res.data
          this.alarmManageStation = res.data[0].stationNum
          this.getAlarmManageData(res.data[0].stationNum)
        }
      })
    },
    handleClick(tab, event) {
      // console.log(tab, event)
    },
    formateTime(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 获取x轴坐标
    getX() {
      if (this.alarmInfo.alertStation && this.alarmInfo.alertStation.length > 0) {
        return this.shuntingAlertOptions.filter(item => this.alarmInfo.alertStation.includes(item.value))
      }
      return []
    },
    async getAllTrainInfos() {
      this.listLoading = true
      this.globalTableData = []
      // 如果时间为空则不进行格式化
      if (this.alarmInfo.startTime) {
        this.alarmInfo.obj.startTime = this.formateTime(this.alarmInfo.startTime)
      } else {
        this.alarmInfo.obj.startTime = null // 如果为空，传递null或者不传值
      }
      if (this.alarmInfo.endTime) {
        this.alarmInfo.obj.endTime = this.formateTime(this.alarmInfo.endTime)
      } else {
        this.alarmInfo.obj.endTime = null
      }
      this.alarmInfo.obj.stationName = this.alarmInfo.stationName
      this.alarmInfo.obj.alertStation = this.alarmInfo.alertStation.join(',')
      this.alarmInfo.obj.trainNo = this.alarmInfo.trainNo

      // 确保没有空值，避免提交空的请求
      const { stationName, alertStation, trainNo, startTime, endTime } = this.alarmInfo.obj
      if (!startTime || !endTime) {
        this.$message.warning('开始时间和结束时间是必填项！')
        return
      }
      const obj = JSON.stringify(this.alarmInfo.obj)
      const params = Object.assign(
        {},
        {
          page: this.alarmInfo.page,
          size: this.alarmInfo.size,
          obj
        }
      )
      // this.queryData.page = this.currentPage.AL
      // this.queryData.limit = this.pageSize
      const res = await getAlarmPageInfos(params)
      if (res.msg) {
        this.$message.info(res.msg)
      } else {
        this.totalDataLength = res.data.total
        this.rcvTableData = res.data.records
      }
      this.xAxis = this.getX()
      this.listLoading = false
    },
    async getAllStationNames() {
      this.stationNameOptions = []
      const res = await getStationNames()
      if (res.msg) {
        this.$message.info(res.msg)
      } else {
        this.stationNameOptions = res.data
      }
    },
    async getAllShuntingAlertOptions() {
      this.shuntingAlertOptions = []
      const res = await getShuntingAlertOptions()
      if (res.msg) {
        this.$message.info(res.msg)
      } else {
        this.shuntingAlertOptions = res.data
      }
    },
    handleCheckboxChange(value) {
      this.getAllTrainInfos()
    },
    reset() {
      this.alarmInfo.startTime = new Date(new Date().getTime() - 3600 * 1000 * 24)
      this.alarmInfo.endTime = new Date(new Date().getTime())
      this.alarmInfo.stationName = ''
      // this.alarmInfo.workNo = ''
      this.alarmInfo.trainNo = ''
      this.getAllTrainInfos()
    },
    search() {
      this.getAllTrainInfos()
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
      this.alarmInfo.page = currentPage
      this.getAllTrainInfos()
    },
    handleSizeChange: function(size) {
      this.pageSize = size
      this.alarmInfo.size = size
      this.getAllTrainInfos()
    }
  }
}
